package com.example.zhenyi;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.stage.Stage;

import java.io.IOException;

public class LoginController {

    @FXML
    private Button cancleButton;

    @FXML
    private Button loginButton;

    @FXML
    private TextField loginName;

    @FXML
    private PasswordField loginPassword;
    @FXML
    private Label tipLabel;

    @FXML
    void cancleButton(ActionEvent event) {

    }

    @FXML
    void loginButton(ActionEvent event) throws IOException {
        login();
    }

    @FXML
    void initialize(){
        //tipLabel.setText("登录失败，用户名或密码错误");

    }

    @FXML
    void loginKeyEnter(KeyEvent event) throws IOException {
        if(event.getCode() == KeyCode.ENTER) {
            // 处理回车事件的逻辑
            login();
        }
    }

    private void login() throws IOException {
        String userName = loginName.getText();
        String password = loginPassword.getText();

        // 在实际应用中，你需要连接数据库进行用户验证
        if (userName.equals("cyj") && password.equals("666")) {
            tipLabel.setText("登录成功");
            // 切换到主界面
            Stage primaryStage=(Stage)loginButton.getScene().getWindow();//将submit(登录按钮)与MainApplication类中的primaryStage(新窗口)绑定 并执行close()
            primaryStage.close();//打开新的窗口 所以要关闭当前的窗口
            ExportMain mainApplication = new ExportMain();//新窗口类
            mainApplication.mainApp();//打开新窗口
        } else {
            tipLabel.setText("登录失败，用户名或密码错误");
        }
    }
}
